package com.weharvest.edu.utlis;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Random;
import java.util.UUID;

public class KeyUtil {

    private static final String salt = "weharvest-edu-丰诚教育";

    public static String genUniqueKey() {
        Random random = new Random();
        //最多生成7位数
        Integer number = random.nextInt(999999) + 999999;
        //生成12位数
        String str = getRandomStr(12);
        return System.currentTimeMillis() + str + String.valueOf(number);
    }

    private static String getRandomStr(int length) {
        String base = "abcdefghijklmnopqrstuvwxyz123456789";
        int randomNum;
        char randomChar;
        Random random = new Random();
        // StringBuffer类型的可以append增加字符
        StringBuffer str = new StringBuffer();

        for (int i = 0; i < length; i++) {
            // 可生成[0,n)之间的整数，获得随机位置
            randomNum = random.nextInt(base.length());
            // 获得随机位置对应的字符
            randomChar = base.charAt(randomNum);
            // 组成一个随机字符串
            str.append(randomChar);
        }
        return str.toString();
    }

    /**
     * 加密密码
     *
     * @return
     */
    public static String getEncryptionPassWord(String key) {
        return DigestUtils.md5Hex(key + salt);
    }

    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("\\-", "");
    }
}
